<%@page import="java.awt.print.Printable"%>
<%@ page contentType="text/html;charset=UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="tags" tagdir="/WEB-INF/tags"%>
<c:set var="ctx" value="${pageContext.request.contextPath}" />
<html>
<head>
<meta HTTP-EQUIV="pragma" CONTENT="no-cache">
<meta HTTP-EQUIV="Cache-Control" CONTENT="no-cache, must-revalidate">
<meta HTTP-EQUIV="expires" CONTENT="0">
<%
response.setHeader("Cache-Control","no-store");
response.setHeader("Pragrma","no-cache");
response.setDateHeader("Expires",0);
%>
<title>_${customer.customerName}_预览作业任务</title>
<script type="text/javascript">

String.prototype.replaceAll = function(s1,s2) { 
    return this.replace(new RegExp(s1,"gm"),s2); 
};

  //创建任务
  function createTask(i){
  
	    //验证是否库存满足 
	    var flag=true;
	    var tempName="tr_"+i;
	    $("[name="+tempName+"]").each(function(){
	        var orderno=$(this).attr("id").substring(3);
	        var stockCheck=$("#stockCheck_"+orderno).val();
	        if(stockCheck=='no'){
	          flag=false;
	          return false;
	        }
	    });
	    
	    if(flag==false){
			$("[name='taskPre']").css("display","none");
			$("#taskPre_"+i).css("display","table");
			$("[name='msg']").html("点击查看");
			$("#msg_"+i).html("点击隐藏");
			var remark = $("#skuList").val();
	       alert("黄色标识订单，"+remark+"库存不足,请核对...");
	       return;
	    }
	    
    //检查是否选择拣货模式
    var pickModel=$("#pickModel_"+i).val();
    if(pickModel == null || pickModel == ''){
       alert("请选择拣货模式...");
       return;
    }
  
    $("#btn_task_"+i).attr("disabled", true);
    var batchCode='${batch.batchCode}';
    //封装拣货库区信息
    var jsonarray_workOrder=[];
    $('[name="task_'+i+'"]').each(function(){
        var ordeno=$(this).attr("id").substring(7);
        var stockInfo=$("#stockInfo_"+ordeno).html().trim().replaceAll('<br>','' );
        var rankno=$("#rankno_"+ordeno).val();
        var obj=createItem(ordeno,stockInfo,rankno);
        jsonarray_workOrder.push(obj);
    });
    var jsonList_workOrder = eval(jsonarray_workOrder); 
	var jsonText_workOrder = JSON.stringify(jsonList_workOrder);
	
	//创建作业任务
	$.ajax({
			type:'post',
			url:'${ctx}/workTask/ajaxCreateTask',
			data:{
				batchCode:batchCode,
				workOrderJsonText:jsonText_workOrder,
				pickModel:pickModel
			},
			dataType:'json',
			success:function(data) {
			    if(data==true){
			      $("#btn_task_"+i).attr("disabled", true);
				  $("#btn_task_"+i).attr("class", "btn");
				  $("#btn_task_"+i).html("已创建任务");
				  
				  
				   //跳转
				  var flag=true;
				  $("[name='btn_createTask']").each(function(){
						 var varClass=$(this).attr("class");
						 if(varClass=='btn btn-primary'){
						     flag=false;
						 }
				  });
				  if(flag==true){
				      /* begin add bug:274  创建作业批次跳转回来后保留查询条件 20160612 lianghe.yuan */
				      
				      //location.href="${ctx}/workBatch/v2_toWorkBatchPage";
				      location.href="${ctx}/workBatch/v2_toWorkBatchPage";
				      
				      /* end add bug:274  创建作业批次跳转回来后保留查询条件 20160612 lianghe.yuan */
				  }
				  
				  
			    }else{
			      $("#btn_task_"+i).attr("disabled", false);
			      alert("网络繁忙，请稍后重试...");
			    }
			   
			},
			error:function() {
		    	$("#btn_task_"+i).attr("disabled",false);
				alert("网络繁忙，请稍后重试...");
			}
	});
  }
  
//创建任务
  function createTask2(i){
  
	    //验证是否库存满足 
	   /*  var flag=true;
	    var tempName="tr_"+i;
	    $("[name="+tempName+"]").each(function(){
	        var orderno=$(this).attr("id").substring(3);
	        var stockCheck=$("#stockCheck_"+orderno).val();
	        if(stockCheck=='no'){
	          flag=false;
	          return false;
	        }
	    });
	    
	    if(flag==false){
			$("[name='taskPre']").css("display","none");
			$("#taskPre_"+i).css("display","table");
			$("[name='msg']").html("点击查看");
			$("#msg_"+i).html("点击隐藏");
			var remark = $("#skuList").val();
	       alert("黄色标识订单，"+remark+"库存不足,请核对...");
	       return;
	    } */
	    
    //检查是否选择拣货模式
    var pickModel=$("#pickModel_"+i).val();
    if(pickModel==''){
       alert("请选择拣货模式...");
       return;
    }
  
    $("#btn2_task_"+i).attr("disabled", true);
    var batchCode='${batch.batchCode}';
    //封装拣货库区信息
    var jsonarray_workOrder=[];
    $('[name="task_'+i+'"]').each(function(){
        var ordeno=$(this).attr("id").substring(7);
        var stockInfo=$("#stockInfo_"+ordeno).html().trim().replaceAll('<br>','' );
        var rankno=$("#rankno_"+ordeno).val();
        var obj=createItem(ordeno,stockInfo,rankno);
        jsonarray_workOrder.push(obj);
    });
    var jsonList_workOrder = eval(jsonarray_workOrder); 
	var jsonText_workOrder = JSON.stringify(jsonList_workOrder);
	
	//创建作业任务
	$.ajax({
			type:'post',
			url:'${ctx}/workTask/ajaxCreateTask',
			data:{
				batchCode:batchCode,
				workOrderJsonText:jsonText_workOrder,
				pickModel:pickModel
			},
			dataType:'json',
			success:function(data) {
			    if(data==true){
			      $("#btn2_task_"+i).attr("disabled", true);
				  $("#btn2_task_"+i).attr("class", "btn");
				  $("#btn2_task_"+i).html("已创建任务");
				  
				  
				   //跳转
				  var flag=true;
				  $("[name='btn2_createTask']").each(function(){
						 var varClass=$(this).attr("class");
						 if(varClass=='btn btn-primary'){
						     flag=false;
						 }
				  });
				  if(flag==true){
				      location.href="${ctx}/workBatch/v2_toWorkBatchPage";
				  }
				  
				  
			    }else{
			      $("#btn2_task_"+i).attr("disabled", false);
			      alert("网络繁忙，请稍后重试...");
			    }
			   
			},
			error:function() {
		    	$("#btn2_task_"+i).attr("disabled",false);
				alert("网络繁忙，请稍后重试...");
			}
	});
  }
  
  //创建json对象
  function createItem(ordeno,stockInfo,rankno){
    return {
        ordeno:ordeno,
        stockInfo:stockInfo,
        rankno:rankno
    };
  }
  
  //显示or隐藏
  function showOrHide(i){
	  var temp=$("#taskPre_"+i);
	  var msg=$("#msg_"+i);
	  
	  if(temp.css("display")=="table"){
		  temp.css("display","none");
		  msg.html("点击查看");
	  }else{
		  $("[name='taskPre']").css("display","none");
		  temp.css("display","table");
		  
		  $("[name='msg']").html("点击查看");
		  msg.html("点击隐藏");
	  }
  }
	//返回未处理订单
  function backUnhandleOrder(orderno) {
	  $.ajax({
			type:'post',
			url:'${ctx}/workTask/ajaxBackUnhandleOrder',
			data:{
				orderno:orderno
			},
			dataType:'json',
			success:function(data) {
			   var result=data.result;
			   if(result=='success'){ //取消成功
				    alert("返回未处理订单成功。");
				    $("#tr_"+orderno).remove();
			   }else if(result=='back'){ //订单已经是未处理订单,不能重复操作
			        alert("订单已经是未处理订单,不能重复操作！");
			        $("#tr_"+orderno).remove();
			   }else if(result=='no_exist'){ //订单不存在
			        alert("订单不存在！");
			        $("#tr_"+orderno).remove();
			   }else{
				    alert("网络繁忙，请稍后重试...");
				}
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	 });
  }
   //取消订单
  function cancelOrder(orderno){
	 $.ajax({
			type:'post',
			url:'${ctx}/cancelOrder/ajaxCancelOrder',
			data:{
				orderno:orderno
			},
			dataType:'json',
			success:function(data) {
			   var msg=data.msg;
 			   if(msg=='success'){ //取消成功
				    alert("订单取消成功。");
				    $("#tr_"+orderno).remove();
 			   }else if(msg=='cancel'){ //不能重复取消
 			        alert("订单已经被取消,不能重复操作！");
 			        $("#tr_"+orderno).remove();
 			   }else{
 				    alert("网络繁忙，请稍后重试...");
 				}
			},
			error:function() {
				alert("网络繁忙，请稍后重试...");
			}
	 });
	 
  }
  
</script>
</head>

<body>
	

<section class="content">
<div class="row">
		<div class="col-md-12">

			<!-- Primary box -->
			<div class="box box-solid box-primary">
				<div class="box-header">
					<i class="fa fa-edit"></i>
					<h3 class="box-title">预览作业任务</h3>
					<div class="box-tools pull-right">
						<button class="btn btn-primary btn-sm" data-widget="collapse">
							<i class="fa fa-minus"></i>
						</button>
					</div>
				</div>
				<div class="box-body">
				
	<table class="table  table-bordered table-condensed" style="margin-top: 0px;">
<!-- 	<thead> -->
<!-- 	   <th style="vertical-align: middle; color: white; height: 30px; " colspan="3" bgcolor="#8EB4CB">预览拣货任务</th> -->
<!-- 	</thead> -->
	<tbody>
	<c:if test="${empty taskList or taskList.size() eq 0}">
	       <tr><td colspan="2" height="50" style="vertical-align:middle; text-align: center;color: gray;font-size: 20px;">无数据</td></tr>
	</c:if>
	
	<c:forEach items="${taskList}" var="t" varStatus="i">
	<tr>
	  <td style='vertical-align: middle;'>
	            <font color="#145565"><b>拣货任务【${i.count}】</b></font><a href='#' style='font-style: italic;margin-left: 10px;' onclick='showOrHide(${i.count});' id='msg_${i.count}' name='msg'>点击查看</a>
	  </td>
	  <td style='width:150px;' >
	  
	  		
		   <select class="form-controls"  id="pickModel_${i.count}" name="single_billType" style="margin-top:8px;">
			        <option value="car">推车拣选</option>  
			        <option value="lamp">灯光拣选</option>
			        <option value="normal">普通拣选</option>
	      </select> 
	  </td>
	  <td style='vertical-align: middle; color: white; width:100px;' >
	     <button  class='btn btn-primary'  onclick='createTask(${i.count})'  id='btn_task_${i.count}' name="btn_createTask" style="width: 110px;">确定创建</button>
	     <shiro:hasAnyRoles name="admin,manager"><button  class='btn btn-primary'  onclick='createTask2(${i.count})'  id='btn2_task_${i.count}' name="btn2_createTask" style="width: 110px;">强制创建</button>
	     </shiro:hasAnyRoles>
	  </td>  
	</tr>
			
	<tr>
	  <td colspan="3">
				<table  class='table table-bordered' style='display:none;'  id='taskPre_${i.count}' name='taskPre'>
				<thead><tr> <th>序号</th> <th>订单号</th> <th>来源店铺</th> <th>SKU编码（商品数量）</th> <th>仓库</th> <th>库位_箱号_sku_数量</th> <th>操作</th> </tr></thead>
				<tbody>
				     <c:forEach items="${t.orderList}" var="o" varStatus="j">
				        <tr id="tr_${o.order.ordeno}" name="tr_${i.count}"
				          <c:if test="${o.stockCheck eq 'no' }"> style="background-color: yellow;" </c:if>
				        > 
					        <td>${j.count}</td> 
					        <td id='ordeno_${o.order.ordeno}' name='task_${i.count}' >${o.order.ordeno}</td> 
					        <td >${o.order.shopna}</td> 
					        <td>
					           <c:forEach items="${o.order.orderItems}" var="orderItem">
					              ${orderItem.sku}（<span name="hidden_${o.order.ordeno}">${orderItem.qty}</span>）;<br/>
					           </c:forEach>
					        </td> 
					        <td>
					           ${o.warehouseCode}
					           <input id="rankno_${o.order.ordeno}" type="hidden" value="${o.rankno}">
					            <input id="cancelStatus_${o.order.ordeno}" type="hidden" value="${o.cancelStatus}">
					           <input id="stockCheck_${o.order.ordeno}" type="hidden" value="${o.stockCheck}">
					           <input id="skuList" type="hidden" value="${skuList}">
					        </td> 
					        <td  id='stockInfo_${o.order.ordeno}' name="stockInfo_${i.count}">${fn:replace(o.stockInfo,';',';<br/>') }</td>
					        
					        <td>
					            <c:if test="${o.stockCheck eq 'no' }">
<%-- 					               <a href="#" onclick="cancelOrder('${o.order.ordeno}'); return false;"  style="font-style: italic;margin-left: 10px;">取消订单</a> --%>
					               <a href="#" onclick="backUnhandleOrder('${o.order.ordeno}'); return false;"  style="font-style: italic;margin-left: 10px;">返回未处理订单</a>
					                
					            </c:if>
					        </td>
					         
				       </tr>
				     </c:forEach>
				</tbody>
				</table>
	  </td>			
	</tr>
			
	</c:forEach>
   
   </tbody>
	</table>
   
</div>
</div>
</div>
</div>
</section>
</body>
</html>
